/*  File Name	: CommonStruct.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-4-5
 *  Description:
 */

#include "CommonStruct.h"
#include "Logger.h"
#include "ComDefine.h"

CommonList::CommonList(int grow_size)
{
	this->grow_size_ = grow_size;
	this->max_size_ = 0;
	this->cur_size_ =0;
	this->item_ptr_ =0;
}

CommonList::~CommonList()
{
	if(this->item_ptr_!=NULL)
	{
		::free(this->item_ptr_);
		this->item_ptr_ = NULL;
	}
}

int CommonList::put_item(VOID_PTR item)
{
	if(NULL==item)
	{
		return -1;
	}
	if(this->cur_size_ < this->max_size_ || this->grow()==0)
	{
		this->item_ptr_[this->cur_size_++]=item;
		return 0;
	}
	return -1;
}

int CommonList::get_item(VOID_PTR &item,int index)
{
	if(index<0 || index >=this->cur_size_ || this->item_ptr_==NULL)
	{
		item =NULL;
		return -1;
	}
	item = this->item_ptr_[index];
	this->item_ptr_[index] = NULL;
	return 0;
}

int CommonList::grow()
{
	if(this->item_ptr_==NULL || this->max_size_==0)
	{
		this->item_ptr_ = (VOID_PTR*)malloc(sizeof(VOID_PTR)*this->grow_size_);

		memset(this->item_ptr_,'\0',sizeof(VOID_PTR)*this->grow_size_);

		this->max_size_ = this->grow_size_;
	}
	else
	{
		this->item_ptr_ =(VOID_PTR*)realloc(this->item_ptr_,
				sizeof(VOID_PTR)*(this->grow_size_+this->max_size_));

		memset(this->item_ptr_+this->max_size_*sizeof(VOID_PTR),
				'\0',sizeof(VOID_PTR)*this->grow_size_);

		this->max_size_ +=this->grow_size_;
	}

	if(this->item_ptr_ ==NULL)
	{
		return -1;
	}
	return 0;
}

void CommonList::reset(void)
{
	this->cur_size_=0;
	memset(this->item_ptr_,0,sizeof(VOID_PTR)*this->max_size_);
}

int CommonList::cur_size(void)
{
	return this->cur_size_;
}

int CommonList::max_size(void)
{
	return this->max_size_;
}

//////////////////////////////////////////////////////////////

DuplexList::DuplexList(int size)
{
	size = (size > 0) ? size : 1024;
	this->read_list_ = new CommonList(size);
	this->write_list_ = new CommonList(size);
	this->cur_read_index_ =0;
}

DuplexList::~DuplexList()
{
	DELETE(this->read_list_);
	DELETE(this->write_list_);
}

int DuplexList::put_obj(VOID_PTR obj)
{
	if(NULL==obj)
	{
		return -1;
	}
	LockGuardReturn(RT_MUTEX,lock,this->write_mutex_,-1);

	return this->write_list_->put_item(obj);
}

int DuplexList::get_obj(VOID_PTR &obj)
{
	LockGuardReturn(RT_MUTEX,lock,this->read_mutex_,-1);
	if(this->read_list_->max_size() > 0 &&
			this->cur_read_index_ < this->read_list_->cur_size())
	{
		return this->read_list_->get_item(obj,this->cur_read_index_++);
	}
	else
	{
		this->swap_list();
		return this->read_list_->get_item(obj,this->cur_read_index_++);
	}
	return 0;
}

int DuplexList::swap_list()
{
	LockGuardReturn(RT_MUTEX,lock,this->write_mutex_,-1);
	CommonList *tmp_list = this->read_list_;
	tmp_list->reset();
	this->read_list_ = this->write_list_;
	this->write_list_ = tmp_list;
	this->cur_read_index_ =0;
	return 0;
}

/////////////////////////////////
PlayerBaseInfo::PlayerBaseInfo()
{
	reset();
}

void PlayerBaseInfo::reset()
{
	role_id_ = 0;
	sex_ = 0;
	name_ = "";
	account_ = "";
}

void PlayerBaseInfo::set_baseinfo(int roleid,std::string account,
		std::string name,int sex,int chips)
{
	this->role_id_ = roleid;
	this->account_ = account;
	this->name_ = name;
	this->sex_ = sex;
	this->chips_ = chips;
}

int PlayerBaseInfo::role_id(void) const
{
	return this->role_id_;
}

void PlayerBaseInfo::role_id(int id)
{
	this->role_id_ = id;
}

int PlayerBaseInfo::sex(void) const
{
	return this->sex_;
}

void PlayerBaseInfo::sex(int sex)
{
	this->sex_ = sex;
}

int PlayerBaseInfo::chips(void) const
{
	return this->chips_;
}

void PlayerBaseInfo::chips(int chips)
{
	this->chips_ = chips;
}

std::string PlayerBaseInfo::account(void) const
{
	return this->account_;
}

void PlayerBaseInfo::account(std::string account)
{
	this->account_ = account;
}

std::string PlayerBaseInfo::name(void) const
{
	return this->name_;
}

void PlayerBaseInfo::name(std::string name)
{
	this->name_ = name;
}

////////////////////////////////////
void DBInfo::reset(void)
{
	ip="";
	dbname = "";
	user = "";
	pwd = "";
	port = 0;
}
void DBInfo::set(std::string ip,int port,std::string db,
		std::string user,std::string pwd)
{
	this->ip = ip;
	this->port = port;
	this->dbname = db;
	this->user = user;
	this->pwd =pwd;
}
